package com.ssy.lingxi.product.controller.pc.customer;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.product.api.model.request.CustomerAttributeRequest;
import com.ssy.lingxi.product.api.model.request.SimpleIdListRequest;
import com.ssy.lingxi.product.api.model.request.SimpleIdRequest;
import com.ssy.lingxi.product.api.model.request.SimpleStatusRequest;
import com.ssy.lingxi.product.api.model.response.CustomerAttributeResponse;
import com.ssy.lingxi.product.common.tree.AttributeNode;
import com.ssy.lingxi.product.entity.customer.CustomerAttribute;
import com.ssy.lingxi.product.model.request.CommodityGoodsAttributeRequest;
import com.ssy.lingxi.product.model.resp.CommodityGoodsAttributeAndBrandResponse;
import com.ssy.lingxi.product.model.resp.CommodityGoodsAttributeResponse;
import com.ssy.lingxi.product.service.customer.ICustomerAttributeService;
import org.modelmapper.TypeToken;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @menu 商品能力--属性管理
 * 商品能力--属性管理
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/23
 */
@RestController
@RequestMapping("/product/customer")
public class CustomerAttributeController extends BaseController {
    
    @Resource
    private ICustomerAttributeService customerAttributeService;

    /**
     * 同步平台后台属性
     * @param simpleIdListRequest 平台后台属性id集合
     * @return
     */
    @RequestMapping(value = "/syncAttribute", method = RequestMethod.POST)
    public Wrapper<Boolean> syncAttribute(@RequestBody SimpleIdListRequest simpleIdListRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(customerAttributeService.syncAttribute(sysUser, simpleIdListRequest));
    }

    /**
     * 查询属性树
     * @param filterInput 是否需要过滤输入类型
     * @return Wrapper<List<Node>>
     */
    @RequestMapping(value = "/getCustomerAttributeTree", method = RequestMethod.GET)
    public Wrapper<List<AttributeNode>> getCustomerAttributeTree(@RequestParam(value = "filterInput", required = false) Boolean filterInput) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(customerAttributeService.getCustomerAttributeTree(sysUser, filterInput));
    }

    /**
     * 获取可选的属性
     *
     * @param commodityGoodsAttributeRequest
     * @return
     */
    @PostMapping("/getEffectiveAttribute")
    public Wrapper<CommodityGoodsAttributeAndBrandResponse> getEffectiveAttribute(@RequestBody @Valid CommodityGoodsAttributeRequest commodityGoodsAttributeRequest) {
        return Wrapper.success(customerAttributeService.getEffectiveAttribute(commodityGoodsAttributeRequest));
    }

    /**
     * 查询属性信息
     * @param id 属性id
     * @return Wrapper<CustomerAttribute>
     */
    @RequestMapping(value = "/getCustomerAttribute", method = RequestMethod.GET)
    public Wrapper<CustomerAttributeResponse> getCustomerAttribute(@RequestParam("id") Long id) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        CustomerAttribute customerAttribute = customerAttributeService.getCustomerAttribute(sysUser, id);
        if(customerAttribute != null){
            return Wrapper.success(this.modelMapper.map(customerAttribute, CustomerAttributeResponse.class));
        }else{
            return Wrapper.success(null);
        }
    }

    /**
     * 查询属性列表
     * @param pageVO 分页实体
     * @param name 属性名称
     * @return Wrapper<PageData<CustomerAttribute>>
     */
    @RequestMapping(value = "/getCustomerAttributeList", method = RequestMethod.GET)
    public Wrapper<PageData<CustomerAttributeResponse>> getCustomerAttributeList(PageVO pageVO,@RequestParam("name") String name) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Page<CustomerAttribute> result = customerAttributeService.getCustomerAttributeList(sysUser, pageVO, name);
        List<CustomerAttributeResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<CustomerAttributeResponse>>(){}.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 新增/修改属性
     * @param customerAttributeRequest 属性实体
     * @return
     */
    @RequestMapping(value = "/saveOrUpdateCustomerAttribute", method = RequestMethod.POST)
    public Wrapper<String> saveOrUpdateCustomerAttribute(@RequestBody CustomerAttributeRequest customerAttributeRequest){
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(customerAttributeService.saveOrUpdateCustomerAttribute(sysUser, this.modelMapper.map(customerAttributeRequest, CustomerAttribute.class)));
    }

    /**
     * 删除属性
     * @param simpleIdRequest
     * @return
     */
    @RequestMapping(value = "/deleteCustomerAttribute", method = RequestMethod.POST)
    public Wrapper<String> deleteCustomerAttribute(@RequestBody SimpleIdRequest simpleIdRequest){
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(customerAttributeService.deleteCustomerAttribute(sysUser, simpleIdRequest.getId()));
    }

    /**
     * 启用/停用属性
     * @param simpleStatusRequest
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateCustomerAttributeStatus", method = RequestMethod.POST)
    public Wrapper<String> updateCustomerAttributeStatus(@RequestBody SimpleStatusRequest simpleStatusRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        return Wrapper.success(customerAttributeService.updateCustomerAttributeStatus(sysUser, simpleStatusRequest.getId(), simpleStatusRequest.getIsEnable()));
    }
}
